<%--
  Created by IntelliJ IDEA.
  User: asus
  Date: 2022/5/9
  Time: 23:58
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<html>
<head>
    <title>RequestBody传递JSON类型的参数</title>
    <script src="./js/jquery-3.4.1.min.js"></script>
    <script src="./js/bootstrap.js"></script>
    <link rel="stylesheet" href="./css/bootstrap.css">
</head>
<body>
<button onclick="sendAjax()" class="btn btn-success">发送JSON数据到后端</button>

<script>
    function sendAjax() {
        var user= {id:1,name:"胡长攀",age:20};
        let userDate = JSON.stringify(user);

        //使用ajax发送json数据到后端控制器
        $.ajax({
            url:"${pageContext.request.contextPath}/ajax/test1",
            type:"post",
            contentType:"application/json",
            data:userDate,//发送的json类型参数
            success:function (res) {//请求成功后的回调函数
                alert(res);
            }
        });

    }
</script>
</body>
</html>
